/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package bean;

import ejb.Ship;
import ejb.ShipFacadeLocal;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 *
 * @author longdm0708l
 */
public class ShipBean {

    public void createShip(Ship ship)
    {
        lookupShipFacade().create(ship);
    }

    public void editShip(Ship ship)
    {
        lookupShipFacade().edit(ship);
    }

    public void removeShip(Ship ship)
    {
        lookupShipFacade().remove(ship);
    }

    private int ShipId;

    public int getShipId() {
        return ShipId;
    }

    public void setShipId(int ShipId) {
        this.ShipId = ShipId;
    }
    
    public List<Ship> getAllShip()
    {
        return lookupShipFacade().findAll();
    }
    
    public Ship getShipById()
    {
        return lookupShipFacade().find(Integer.valueOf(getShipId()));
    }

    private ShipFacadeLocal lookupShipFacade() {
        try {
            Context c = new InitialContext();
            return (ShipFacadeLocal) c.lookup("java:comp/env/ShipFacade");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

}
